<?php
	require_once('include/lsamp_curl.php');
	require_once('config.inc.php');
	$http_client = new lsamp_cURL(TRUE, 'cookies.dat', 'gzip', '', '', 30);
	if(isset($_POST)){
		$dom_account['dom'] = $_POST['dom'];
		$dom_account['cpanel_user'] = $_POST['cpanel_username'];
		$dom_account['password'] = $_POST['password'];
	}
	// login cpanel
	$login_url = $cpanel_login_url
				. "?login=" . $dom_account['cpanel_user']
				. "&password=" . $dom_account['password'];
	$result = $http_client->get($login_url, $errno, $error);
	if($errno != 0){
		echo date("d/m/y H:i:s") . " | " . "Error: " . $error . "<br>";
		exit;
	}else{
	if(strstr($result, "Login Failed") !== false){
			echo date("d/m/y H:i:s") .  " | " ."Failed to login Cpanel<br>";
		}else{
			echo date("d/m/y H:i:s") .  " | " ."Logged in Cpanel<br>";
		}
	}
//	$result = $http_client->get($cpanel_login_redirect_url, $errno, $error);
//	if($errno != 0){
//		echo date("d/m/y H:i:s") . " | " . "Error: " . $error . "<br>";
//	}else{
//		echo date("d/m/y H:i:s") .  " | " ."Logged in Cpanel<br>";
//	}

	// login domain manager
	$login_data = "l_redirect=/cgi/hosting"
			. "&l_expires_min=0"
			. "&ldomain=" . $dom_account['dom']
			. "&lpass=" . $dom_account['password'];
	$result = $http_client->post($dom_manager_login_url, $login_data, $errno, $error);
	if($errno != 0){
		echo date("d/m/y H:i:s") . " | " . "Error: " . $error . "<br>";
	}else{
		if(strstr($result, "Login Failed") !== false){
			echo date("d/m/y H:i:s") .  " | " ."Failed to login Domain Manager<br>";
		}else{
			echo date("d/m/y H:i:s") .  " | " ."Logged in Domain Manager<br>";
		}
		$html_dom_list = '<h1>Domain List:</h1><br>';
		// parse hosting domain info
		if(!preg_match_all('#<td class="hcol_1"><div>([^<]+)</div>#mis',$result, $name_match_array)){
			echo date("d/m/y H:i:s") .  " | " ."Failed to get domain list<br>";
		}else{
			for($i=1; $i < count($name_match_array[1]); $i++){
				$html_dom_list .= $name_match_array[1][$i] . "<br>";
			}
//			if(preg_match_all('/<td class=\"hcol_2\"><div><span style=\"float: left;\"><a id=\"tip_\d+\" href=\"([^"]+)\">([^<]+)/is',$result, $url_status_match_array)){
//				var_dump($url_status_match_array[2]);
//				for($i=0; $i < count($name_match_array[1]); $i++){
//					$html_dom_list .= "<a href=\"" . $url_status_match_array[1][$i] ."\">" . $name_match_array[1][$i]
//								. "(" . $url_status_match_array[2][$i] . ")"
//								. "</a><br>";
//				}
//			}
			echo $html_dom_list;

		}
	}
	echo "<div><hr><h3>Check domain: " . $_POST['dom_to_check'] . "</h3></div>";
	if(isset($_POST['dom_to_check'])){
		$check_domain_data = "domain=" . $_POST['dom_to_check'];
		$result = $http_client->post($check_domain_url, $check_domain_data, $errno, $error);

		if($errno != 0){
			echo date("d/m/y H:i:s") . " | " . "Error: " . $error . "<br>";
		}else{
			if(preg_match('/\(\{([^{]+)\}\)/mis', $result, $matches)!==false){
				echo "<div>" . $matches[1] . "</div>";
			}
		}
	}




